<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="字段名" prop="columnName">
        <a-input v-model="form.columnName" placeholder="请输入字段名，请使用下划线方式命名" />
      </a-form-model-item>
      <a-form-model-item label="字段描述" prop="columnComment">
        <a-input v-model="form.columnComment" placeholder="请输入字段描述" />
      </a-form-model-item>
      <a-form-model-item label="字段类型" prop="columnType">
        <a-cascader expand-trigger="hover" :options="options" placeholder="请选择字段类型" @change="onTypeChange" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>

import { addGenTable } from '@/api/tool/gen'

export default {
  name: 'CreateColumnForm',
  components: {
  },
  data () {
    return {
      loading: false,
      menuExpandedKeys: [],
      autoExpandParent: false,
      menuCheckedKeys: [],
      halfCheckedKeys: [],
      menuOptions: [],
      formTitle: '',
      // 表单参数
      form: {
        columnName: undefined,
        columnComment: undefined,
        columnType: undefined
      },
      open: false,
      rules: {
        columnName: [{ required: true, message: '字段名称不能为空', trigger: 'blur' }],
        columnComment: [{ required: true, message: '字段描述不能为空', trigger: 'blur' }],
        columnType: [{ required: true, message: '字段类型不能为空', trigger: 'blur' }]
      },
      options: [
        {
          value: 'input',
          label: '文本',
          children: [{
            value: 'varchar(2)',
            label: 'varchar(2)'
          }, {
            value: 'varchar(8)',
            label: 'varchar(8)'
          }, {
            value: 'varchar(32)',
            label: 'varchar(32)'
          }, {
            value: 'varchar(64)',
            label: 'varchar(64)'
          }, {
            value: 'varchar(128)',
            label: 'varchar(128)'
          }, {
            value: 'varchar(255)',
            label: 'varchar(255)'
          }, {
            value: 'text',
            label: '不限'
          }]
        },
        {
          value: 'number',
          label: '数字',
          children: [{
              value: 'smallint',
              label: 'smallint'
            }, {
              value: 'int',
              label: 'int'
            }, {
              value: 'bigint',
              label: 'bigint'
            }
          ]
        }, {
          value: 'clock',
          label: '日历',
          children: [{
            value: 'date',
            label: '日期'
          }, {
            value: 'datetime',
            label: '日期-时间'
          }]
        }
      ]
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        columnName: undefined,
        columnComment: undefined
      }
    },
    onTypeChange (val) {
      this.form.columnType = val[val.length - 1]
    },
     /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '增加表字段'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          addGenTable(this.form).then(res => {
            this.$message.success(
              '新增成功',
              3
            )
            this.open = false
            this.$emit('ok')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
